/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;


import Entities.SACH;
import Entities.TACGIA;
import Entities.THELOAI;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.swing.JOptionPane;

/**
 * FXML Controller class
 *
 * @author lainh_000
 */
public class FX_TimKiem_SachController implements Initializable {

    
    //Khai báo control
    @FXML Button btn_ok;
    @FXML Button btn_huy;
    @FXML TextField txt_tensach;
    @FXML TextField txtGiaTu;
    @FXML TextField txtGiaDen;
    @FXML TextField txt_Masach;
    @FXML TextField txt_SoLuong;
    @FXML Label lb;
    @FXML ImageView img;
    @FXML ListView list_theloai;
    @FXML ListView list_tacgia;
    @FXML CheckBox CheckBox_TenSach;
    @FXML CheckBox CheckBox_theloai;
    @FXML CheckBox CheckBox_tacgia;
    @FXML CheckBox CheckBox_DonGia;
    @FXML CheckBox CheckBox_MaSach;
    @FXML CheckBox CheckBox_SoLuong;
    //final ObservableList<THELOAI> data_tl = FXCollections.observableArrayList(); 
    //public int ma_theloai, ma_tacgia;
    String masach_key,ma_tacgia,ma_theloai,tensach_key,giatu_key,giaden_key,soluong_key="";
    
    
    SACH sach_model = new SACH();
    THELOAI theloai_model = new THELOAI();
    TACGIA tacgia_model = new TACGIA();
    /**
     * Initializes the controller class.
     */
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        KhoiTao();

        LoadDataList_theloai();
        
        LoadDataList_tacgia();
    }
    
    public void btn_TimKiem_Clicked(ActionEvent event)
    {
        getData();
        
        sach_model.Search_Sach(masach_key,tensach_key,ma_tacgia,ma_theloai,giatu_key,giaden_key,soluong_key);
        
        Stage stage = (Stage) btn_ok.getScene().getWindow();
        stage.close();
    }
    
    public void getData()
    {
        // Mã
        if(CheckBox_MaSach.isSelected()== true)
        {
            masach_key= txt_Masach.getText();
            // Nếu tìm theo mã thì disable các checkbox còn lại
            CheckBox_TenSach.setDisable(true);
            CheckBox_theloai.setDisable(true);
            CheckBox_tacgia.setDisable(true);
            CheckBox_DonGia.setDisable(true);
            CheckBox_SoLuong.setDisable(true);
            
        }
        else
            masach_key="";
        // Tên
        if(CheckBox_TenSach.isSelected()== true)
        {
            tensach_key= txt_tensach.getText();
        }
        else
            tensach_key="";
        // Thể loại
        if(CheckBox_theloai.isSelected()== false)
        {
            ma_theloai= "";
        }
        // Tác giả
        if(CheckBox_tacgia.isSelected()== false)
        {
            ma_tacgia= "";
        }
        // Đơn giá từ
        if(CheckBox_DonGia.isSelected() == true)
        {
            giatu_key = txtGiaTu.getText();
            giaden_key = txtGiaDen.getText();
            if("".equals(giatu_key) || "".equals(giaden_key))
            {
                String stb="Vui lòng điền giá từ và giá đến";
                JOptionPane.showMessageDialog(null,stb);
            }
        }
        else
        {
            giatu_key="";
            giaden_key="";
        }
        
        // Số lượng
        if(CheckBox_SoLuong.isSelected() == true)
        {
            soluong_key = txt_SoLuong.getText();
        }
        else
            soluong_key="";
    }
    
    public void btn_Huy_Clicked(ActionEvent event)
    {
        Stage stage = (Stage) btn_huy.getScene().getWindow();
        stage.close();
    }
    
    public void CheckBox_Masach_Checked(ActionEvent event)
    {
        if(CheckBox_MaSach.isSelected()== true)
        {
            
            txt_Masach.setDisable(false);
        }
        else
          txt_Masach.setDisable(true);
    }
    
    public void CheckBox_TenSach_Checked(ActionEvent event)
    {
        if(CheckBox_TenSach.isSelected()== true)
        {
            txt_tensach.setDisable(false);
        }
        else
          txt_tensach.setDisable(true);
    }
    
    public void CheckBox_TheLoai_Checked(ActionEvent event)
    {
        if(CheckBox_theloai.isSelected()== true)
        {
            list_theloai.setDisable(false);
        }
        else
          list_theloai.setDisable(true);
    }
    
    public void CheckBox_TacGia_Checked(ActionEvent event)
    {
        if(CheckBox_tacgia.isSelected()== true)
        {
            list_tacgia.setDisable(false);
        }
        else
          list_tacgia.setDisable(true);
    }
    
    public void CheckBox_SoLuong_Checked(ActionEvent event)
    {
        if(CheckBox_SoLuong.isSelected()== true)
        {
            txt_SoLuong.setDisable(false);
        }
        else
          txt_SoLuong.setDisable(true);
    }
    
    public void CheckBox_DonGia_Checked(ActionEvent event)
    {
        if(CheckBox_DonGia.isSelected()== true)
        {
            txtGiaTu.setDisable(false);
            txtGiaDen.setDisable(false);
        }
        else
        {
            txtGiaTu.setDisable(true);
            txtGiaDen.setDisable(true);
        }
    }
    
    private void KhoiTao()
    {
        assert btn_ok != null;
        assert btn_huy != null;
        assert txt_tensach != null;
        assert txtGiaTu !=null;
        assert txtGiaDen !=null;
        assert list_theloai !=null;
        assert list_tacgia !=null;
        assert CheckBox_theloai !=null;
        assert CheckBox_tacgia !=null;
        assert txt_Masach !=null;
        
        // Định nghĩa dữ liệu được load lên list (tên thể loại)
        list_theloai.setCellFactory(new Callback<ListView<THELOAI>, ListCell<THELOAI>>(){
 
            @Override
            public ListCell<THELOAI> call(ListView<THELOAI> p) {       
                ListCell<THELOAI> cell = new ListCell<THELOAI>(){
                    @Override
                    protected void updateItem(THELOAI t, boolean bln) {
                        super.updateItem(t, bln);
                        if (t != null) {
                            // SetText cho list là Tên thể loại
                            setText(t.getMatl() +"-" +t.getTentl());
                        }
                    }
                };
                return cell;
            }
        });
        
        // Định nghĩa dữ liệu được load lên list (tên tác giả)
        list_tacgia.setCellFactory(new Callback<ListView<TACGIA>, ListCell<TACGIA>>(){
 
            @Override
            public ListCell<TACGIA> call(ListView<TACGIA> p) {       
                ListCell<TACGIA> cell = new ListCell<TACGIA>(){
                    @Override
                    protected void updateItem(TACGIA t, boolean bln) {
                        super.updateItem(t, bln);
                        if (t != null) {
                            // SetText cho list là Tên tác giả
                            setText(t.getMatg() + "-" +t.getTentg());
                        }
                    }
                };
                return cell;
            }
        });
        
        
        // Code xử lý khi select 1 item trong list The Loai
        list_theloai.getSelectionModel().selectedItemProperty().addListener(
            new ChangeListener<THELOAI>() {
                public void changed(ObservableValue<? extends THELOAI> ov, 
                    THELOAI old_val, THELOAI new_val) {
                        ma_theloai = String.valueOf(new_val.getMatl());
                        
            }
        });
        
        // Code xử lý khi select 1 item trong list Tac Gia
        list_tacgia.getSelectionModel().selectedItemProperty().addListener(
            new ChangeListener<TACGIA>() {
                public void changed(ObservableValue<? extends TACGIA> ov, 
                    TACGIA old_val, TACGIA new_val) {
                        ma_tacgia = String.valueOf(new_val.getMatg());
                      
            }
        });
    }
    
    
    private void LoadDataList_theloai()
    {
        //theloai_model.getTheLoai_All();
        // Gán data lên list
        list_theloai.setItems(THELOAI.theloai_items);
    }   
    
    private void LoadDataList_tacgia()
    {
        //tacgia_model.getTacGia_All();
        // Gán data lên list
        list_tacgia.setItems(TACGIA.tacgia_items);
    }
    
    
    
}
